const { login, getInfo } = require('../../utils/api');
Page({
  data: {
    username: '',
    password: '',
  },

  handleInput(e) {
    const { type } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [type]: value,
    });
  },

  handleLogin() {
    const { username, password } = this.data;
    const params = { username, password };
    login(params).then((res) => {
      wx.showToast({ title: '登录成功' });
      wx.setStorageSync('roleKey', res.roleKey);
      wx.setStorageSync('token', res.token);
      getInfo().then((result) => {
        wx.setStorageSync('userInfo', result?.user || {});
        wx.reLaunch({ url: '/pages/index/index' });
      });
    });
  },
});
